package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Schedule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.FitnessplansMapper;
import com.ruoyi.system.domain.Fitnessplans;
import com.ruoyi.system.service.IFitnessplansService;

/**
 * 健身计划信息Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-27
 */
@Service
public class FitnessplansServiceImpl implements IFitnessplansService 
{
    @Autowired
    private FitnessplansMapper fitnessplansMapper;

    /**
     * 查询健身计划信息
     * 
     * @param fitnessPlanId 健身计划信息主键
     * @return 健身计划信息
     */
    @Override
    public Fitnessplans selectFitnessplansByFitnessPlanId(Long fitnessPlanId)
    {
        return fitnessplansMapper.selectFitnessplansByFitnessPlanId(fitnessPlanId);
    }

    /**
     * 查询健身计划信息列表
     * 
     * @param fitnessplans 健身计划信息
     * @return 健身计划信息
     */
    @Override
    public List<Fitnessplans> selectFitnessplansList(Fitnessplans fitnessplans)
    {
        return fitnessplansMapper.selectFitnessplansList(fitnessplans);
    }

    /**
     * 新增健身计划信息
     * 
     * @param fitnessplans 健身计划信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertFitnessplans(Fitnessplans fitnessplans)
    {
        fitnessplans.setCreateTime(DateUtils.getNowDate());
        int rows = fitnessplansMapper.insertFitnessplans(fitnessplans);
        insertSysUser(fitnessplans);
        return rows;
    }

    /**
     * 修改健身计划信息
     * 
     * @param fitnessplans 健身计划信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateFitnessplans(Fitnessplans fitnessplans)
    {
        fitnessplans.setUpdateTime(DateUtils.getNowDate());
        fitnessplansMapper.deleteSysUserByUserId(fitnessplans.getFitnessPlanId());
        insertSysUser(fitnessplans);
        return fitnessplansMapper.updateFitnessplans(fitnessplans);
    }

    /**
     * 批量删除健身计划信息
     * 
     * @param fitnessPlanIds 需要删除的健身计划信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFitnessplansByFitnessPlanIds(Long[] fitnessPlanIds)
    {
        fitnessplansMapper.deleteSysUserByUserIds(fitnessPlanIds);
        return fitnessplansMapper.deleteFitnessplansByFitnessPlanIds(fitnessPlanIds);
    }

    /**
     * 删除健身计划信息信息
     * 
     * @param fitnessPlanId 健身计划信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteFitnessplansByFitnessPlanId(Long fitnessPlanId)
    {
        fitnessplansMapper.deleteSysUserByUserId(fitnessPlanId);
        return fitnessplansMapper.deleteFitnessplansByFitnessPlanId(fitnessPlanId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param fitnessplans 健身计划信息对象
     */
    public void insertSysUser(Fitnessplans fitnessplans)
    {
        List<SysUser> sysUserList = fitnessplans.getSysUserList();
        Long fitnessPlanId = fitnessplans.getFitnessPlanId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(fitnessPlanId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                fitnessplansMapper.batchSysUser(list);
            }
        }
    }
    /**
     * 新增健身计划推荐信息
     *
     * @param fitnessplans 健身计划信息对象
     */
    public void insertSchedule(Fitnessplans fitnessplans)
    {
        List<Schedule> scheduleList = fitnessplans.getScheduleList();
        Long fitnessPlanId = fitnessplans.getFitnessPlanId();
        if (StringUtils.isNotNull(scheduleList))
        {
            List<Schedule> list = new ArrayList<Schedule>();
            for (Schedule schedule : scheduleList)
            {
                schedule.setScheduleId(fitnessPlanId);
                list.add(schedule);
            }
            if (list.size() > 0)
            {
                fitnessplansMapper.batchSchedule(list);
            }
        }
    }
}
